//
//  StatusCell.m
//  OC-微博
//
//  Created by Apple on 15/11/13.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "StatusCell.h"
#import "StatuPictureView.h"
#import "Status.h"
#import "StatusViewModel.h"
@interface StatusCell()
//顶部
@property (nonatomic, strong)StatusCellTopView  *topView;
// 微博正文标签
@property (nonatomic, strong) UILabel *contentLabel;
//底部
@property (nonatomic, strong) StatusCellBottomView *bottomView;
@property (nonatomic, strong)StatuPictureView  *pictureView;

@end
@implementation StatusCell
#pragma -  mark /*******************  懒加载  *******************/
- (void)setStatus:(Status *)status {
    _status = status;
    self.contentLabel.text = status.text;
   self.topView.vie = [[StatusViewModel alloc]init:status];
   self.pictureView.viewM = [[StatusViewModel alloc]init:status];
       //配图行高
    [self.pictureView updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.pictureView.bounds.size.height);
        make.width.equalTo(self.pictureView.bounds.size.width);
        //根据配图数量，决定配图视图的顶部间距
        CGFloat offset = self.topView.vie.thumbnailsURL.count > 0 ? StatusCellMargin : 0;
        make.top.equalTo(self.contentLabel.bottom).offset(offset);
    }];
}
//- (void)setViewModel:(StatusViewModel *)ViewModel {
//    _ViewModel = ViewModel;
//    self.contentLabel.text = ViewModel.statu.text;
//    self.topView.vie = ViewModel;
//    self.pictureView.viewM = ViewModel;
//    //配图行高
//    [self.pictureView updateConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(self.pictureView.bounds.size.height);
//        make.width.equalTo(self.pictureView.bounds.size.width);
//        //根据配图数量，决定配图视图的顶部间距
//        CGFloat offset = self.topView.vie.thumbnailsURL.count > 0 ? StatusCellMargin : 0;
//        make.top.equalTo(self.contentLabel.bottom).offset(offset);
//    }];
//
//}
#pragma mark /*******************  计算行高  *******************/
- (CGFloat)rowHeight:(Status *)vm {
    // 1. 记录视图模型
      self.status = vm;
   
    // 2. 强制更新所有约束 -> 所有控件的frame都会被计算正确
    [self.contentView layoutIfNeeded];
    // 3. 返回底部视图的最大高度
    return CGRectGetMaxY(self.bottomView.frame);
}

#pragma mark /*******************  构造函数  *******************/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]) {
       // self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
    
}

#pragma mark /*******************  设置界面  *******************/
- (void)setupUI {
    // 1. 添加控件
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.pictureView];
    [self.contentView addSubview:self.bottomView];
    // 2. 自动布局
    // 1> 顶部视图
    [self.topView makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth);
    }];
    // 2> 内容标签
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.bottom).offset(StatusCellMargin);
        make.left.equalTo(self.contentView.left).offset(StatusCellMargin);
       
        
    }];
    // 3> 配图视图
    [self.pictureView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.bottom).offset(StatusCellMargin);
        make.left.equalTo(self.contentLabel.left);
        make.width.equalTo(300);
        make.height.equalTo(90);
    }];
    // 4> 底部视图
   [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
       make.left.right.equalTo(self.contentView);
       make.top.equalTo(self.pictureView.bottom).offset(StatusCellMargin);
       make.height.equalTo(44);
       
   }];
}
#pragma mark /*******************  懒加载控件  *******************/
-(StatusCellTopView *)topView {
    
    if (_topView == nil) {
        _topView = [StatusCellTopView new];
    }
    return _topView;
}
-(UILabel *)contentLabel {
    
    if (_contentLabel == nil) {
        _contentLabel = [UILabel setLabelTitle:@"微博正文" textColor:[UIColor darkGrayColor] labelFont:15 screenInset:StatusCellMargin];
    }
    return _contentLabel;
}
-(StatusCellBottomView *) bottomView{
    
    if (_bottomView == nil) {
        _bottomView = [StatusCellBottomView new];
    }
    return _bottomView;
}
//懒加载
-(StatuPictureView *)pictureView {
    if (_pictureView == nil) {
        
    
        _pictureView = [[StatuPictureView alloc]init] ;
   
    }
    
    return _pictureView;
}


@end
